package com.tonghl.juc.queue_study.queueutil;

import java.util.concurrent.CountDownLatch;

/**
 * Created by tonghl
 */
   enum CountryEnum{
    ONE(1,"齐国"),TWO(2,"楚国"),THREE(3,"燕国"),FOUR(4,"赵国"),FIVE(5,"魏国"),SIX(6,"韩国");
    private  Integer retCode;
    private  String retMessage;

    public Integer getRetCode() {
        return retCode;
    }

    public void setRetCode(Integer retCode) {
        this.retCode = retCode;
    }

    public String getRetMessage() {
        return retMessage;
    }

    public void setRetMessage(String retMessage) {
        this.retMessage = retMessage;
    }

    CountryEnum(Integer retCode, String retMessage) {
        this.retCode = retCode;
        this.retMessage = retMessage;
    }

    public static CountryEnum forEach_CountryEnum(int index){
        CountryEnum[] myArray = CountryEnum.values();
        for (CountryEnum element: myArray)
        {
            if (index == element.retCode) {
                return  element;
            }  
        }
        return null;
    }
}

public class CountDownLatchDemo {

    public static void main(String[] args)  throws  Exception{

        CountDownLatch countDownLatch=new CountDownLatch(6);//做减法

        for (int i = 1; i <6 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t，被灭");
                countDownLatch.countDown();
            },CountryEnum.forEach_CountryEnum(i).getRetMessage()).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t***********秦一统天下");
        System.out.println(CountryEnum.ONE);
        System.out.println(CountryEnum.ONE.getRetCode());
        System.out.println(CountryEnum.ONE.getRetMessage());

    }

    public static void closeDoor() throws InterruptedException {
        CountDownLatch countDownLatch=new CountDownLatch(6);//做减法

        for (int i = 1; i <6 ; i++) {
          new Thread(()->{
              System.out.println(Thread.currentThread().getName()+"\t上完自习，离开教室");
              countDownLatch.countDown();
          },String.valueOf(i)).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t***********关灯锁门");
    }
}
